package com.css.fxfzypg.modules.auxiliary.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.auxiliary.entity.AuxiliaryMap;
import com.css.fxfzypg.modules.auxiliary.param.MapParam;
import com.css.fxfzypg.modules.auxiliary.param.QueryParam;
import com.css.fxfzypg.modules.auxiliary.service.AuxiliaryMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/auxiliary/map")
public class AuxiliaryMapController {
    @Autowired
    private AuxiliaryMapService auxiliaryMapService;

    @PostMapping("save")
    public RestResponse save(@RequestBody MapParam mapParam) {
        RestResponse restResponse = null;
        try {
            auxiliaryMapService.save(mapParam);
            restResponse = RestResponse.succeed("保存成功");
        } catch (Exception e) {
            restResponse = RestResponse.fail("保存失败");
        }
        return restResponse;
    }

    @GetMapping("getAll")
    public RestResponse getAll(QueryParam queryParam) {
        RestResponse restResponse = null;
        try {
            Map<String, Object> map = auxiliaryMapService.getAll(queryParam);
            restResponse = RestResponse.succeed(map);
        } catch (Exception e) {
            restResponse = RestResponse.fail("查询失败");
        }
        return restResponse;
    }

    @GetMapping("getById/{id}")
    public RestResponse getById(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try {
            AuxiliaryMap auxiliaryMap = auxiliaryMapService.getById(id);
            restResponse = RestResponse.succeed(auxiliaryMap);
        } catch (Exception e) {
            log.error("查询图件数据失败", e);
            restResponse = RestResponse.fail("查询图件数据失败");
        }
        return restResponse;
    }

    @PostMapping("deleteByIds/{ids}")
    public RestResponse deleteByIds(@PathVariable List<String> ids) {
        RestResponse restResponse = null;
        try {
            auxiliaryMapService.deleteByIds(ids);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("删除图件数据失败");
            restResponse = RestResponse.fail("删除图件数据失败");
        }
        return restResponse;
    }

    @PostMapping("/update")
    public RestResponse update(@RequestBody AuxiliaryMap auxiliaryMap) {
        RestResponse restResponse = null;
        try {
            auxiliaryMapService.update(auxiliaryMap);
            restResponse = RestResponse.succeed("修改图件数据成功");
        } catch (Exception e) {
            log.error("修改图件数据失败");
            restResponse = RestResponse.fail("修改图件数据失败");
        }
        return restResponse;
    }

    //校验:同一个地震同一个图件类型需要覆盖
    //返回 true 表示有数据,需要覆盖
    //返回 flase 表示没有数据,不需要覆盖
    @PostMapping("check")
    public RestResponse check(@RequestBody MapParam mapParam) {
        RestResponse restResponse = null;
        try {
            Boolean flag = auxiliaryMapService.check(mapParam);
            restResponse = RestResponse.succeed(flag);
        } catch (Exception e) {
            restResponse = RestResponse.fail("校验失败");
        }
        return restResponse;
    }
}
